package com.ruoyi.project.poem.posterad.controller;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.poem.posterad.domain.PosterAd;
import com.ruoyi.project.poem.posterad.service.IPosterAdService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 诗词广告位Controller
 *
 * @author shixiangwen
 * @date 2021-07-14
 */
@Controller
@RequestMapping("/poem/posterad")
public class PosterAdController extends BaseController {

    private String prefix = "poem/posterad";

    @Autowired
    private IPosterAdService posterAdService;

    @RequiresPermissions("poem:posterad:view")
    @GetMapping()
    public String posterad() {
        return prefix + "/posterad";
    }

    /**
     * 查询诗词广告位列表
     */
    @RequiresPermissions("poem:posterad:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(PosterAd posterAd) {
        startPage();
        List<PosterAd> list = posterAdService.selectPosterAdList(posterAd);
        return getDataTable(list);
    }

    /**
     * 导出诗词广告位列表
     */
    @RequiresPermissions("poem:posterad:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(PosterAd posterAd) {
        List<PosterAd> list = posterAdService.selectPosterAdList(posterAd);
        ExcelUtil<PosterAd> util = new ExcelUtil<PosterAd>(PosterAd.class);
        return util.exportExcel(list, "posterad");
    }

    /**
     * 新增诗词广告位
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存诗词广告位
     */
    @RequiresPermissions("poem:posterad:add")
    @Log(title = "诗词广告位", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(PosterAd posterAd) {
        return toAjax(posterAdService.insertPosterAd(posterAd));
    }

    /**
     * 修改诗词广告位
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        PosterAd posterAd = posterAdService.selectPosterAdById(id);
        mmap.put("posterAd", posterAd);
        return prefix + "/edit";
    }

    /**
     * 修改保存诗词广告位
     */
    @RequiresPermissions("poem:posterad:edit")
    @Log(title = "诗词广告位", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(PosterAd posterAd) {
        return toAjax(posterAdService.updatePosterAd(posterAd));
    }

    /**
     * 删除诗词广告位
     */
    @RequiresPermissions("poem:posterad:remove")
    @Log(title = "诗词广告位", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(posterAdService.deletePosterAdByIds(ids));
    }

    @RequiresPermissions("poem:posterad:edit")
    @Log(title = "诗词广告位", businessType = BusinessType.GRANT)
    @PostMapping("/updateStatus/{id}")
    @ResponseBody
    public AjaxResult updateStatus(@PathVariable("id") Long id, Integer status) {

        PosterAd posterAd = posterAdService.selectPosterAdById(id);
        posterAd.setStatus(status);

        return toAjax(posterAdService.updatePosterAd(posterAd));
    }
}
